package servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.SignedRequestsHelper;

public class ProductLookup extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private static final String AWS_ACCESS_KEY_ID = "AKIAIWIOLZ7WSLYBCSEA";
	private static final String AWS_SECRET_KEY = "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";
	private static final String ENDPOINT = "webservices.amazon.co.uk";

	protected void doGet(HttpServletRequest request, HttpServletResponse response) {
		String itemID = request.getParameter("itemID");
		
		SignedRequestsHelper helper;
        try {
            helper = SignedRequestsHelper.getInstance(ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
		
        String requestUrl = null;
        
        Map<String, String> params = new HashMap<String, String>();
        params.put("Service", "AWSECommerceService");
        params.put("Version", "2011-08-01");
        params.put("Operation", "ItemLookup");
        params.put("ItemId", itemID);
        params.put("AssociateTag", "wwwmarcoden-21");
        
        requestUrl = helper.sign(params);

        try {
			response.getWriter().write(requestUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
